package cn.org.seventh.web.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

/**
 * @author <a href="mailto:ji.li@symbio-group.com">Andy</a>
 * @since 2007-11-28 下午09:37:12
 * @jsp.tag name="split" body-content="empty" description="gen split pages"
 */
public class SplitTag implements Tag {

	private String url;

	private String param;

	private String urlStyle;

	private PageContext pageContext;

	/**
	 * @jsp.attribute required="false" rtexprvalue="true" description="set the
	 *                url style"
	 * @param url
	 */
	public void setUrlStyle(String urlStyle) {
		this.urlStyle = urlStyle;
	}

	/**
	 * @jsp.attribute required="true" rtexprvalue="true" description="set the
	 *                url"
	 * @param url
	 */
	public void setUrl(String url) {
		try {
			this.url = (String) ExpressionEvaluatorManager.evaluate("url", url,
					String.class, this, pageContext);
		} catch (JspException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @jsp.attribute required="false" rtexprvalue="true" description="set the
	 *                param"
	 * @param param
	 */
	public void setParam(String param) {
		try {
			this.param = (String) ExpressionEvaluatorManager.evaluate("param",
					param, String.class, this, pageContext);
		} catch (JspException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see javax.servlet.jsp.tagext.Tag#doEndTag()
	 */
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	/**
	 * @see javax.servlet.jsp.tagext.Tag#doStartTag()
	 */
	public int doStartTag() throws JspException {
		try {
			Long firstPage = (Long) pageContext.getRequest().getAttribute(
					"firstPage");
			Long lastPage = (Long) pageContext.getRequest().getAttribute(
					"lastPage");
			Long prePage = (Long) pageContext.getRequest().getAttribute(
					"prePage");
			Long nextPage = (Long) pageContext.getRequest().getAttribute(
					"nextPage");
			Long totlePages = (Long) pageContext.getRequest().getAttribute(
					"totlePages");
			Long totleRecords = (Long) pageContext.getRequest().getAttribute(
					"totleRecords");
			Long perPage = (Long) pageContext.getRequest().getAttribute(
					"perPage");
			Long page = (Long) pageContext.getRequest().getAttribute("page");
			StringBuffer msg = new StringBuffer();
			msg.append("&nbsp;第").append(page).append("/").append(totlePages)
					.append("页 ").append("每页").append(perPage).append("条 ")
					.append("共").append(totleRecords).append("条");
			if (null == param) {
				param = "";
			}
			String style = "";
			if (null != urlStyle) {
				style = " class='" + urlStyle + "' ";
			}
			StringBuffer first = new StringBuffer();
			first.append(" <a").append(style).append(" href=\"").append(url)
					.append("?page=").append(firstPage).append(param).append(
							"\">").append("首页").append("</a>");
			StringBuffer pre = new StringBuffer();
			pre.append(" <a").append(style).append(" href=\"").append(url)
					.append("?page=").append(prePage).append(param).append(
							"\">").append("上一页").append("</a>");
			StringBuffer next = new StringBuffer();
			next.append(" <a").append(style).append(" href=\"").append(url)
					.append("?page=").append(nextPage).append(param).append(
							"\">").append("下一页").append("</a>");
			StringBuffer last = new StringBuffer();
			last.append(" <a").append(style).append(" href=\"").append(url)
					.append("?page=").append(lastPage).append(param).append(
							"\">").append("尾页").append("</a>");
			JspWriter out = pageContext.getOut();
			out.write("<div>");
			out.write(msg.append(first).append(pre).append(next).append(last)
					.toString());
			out.write("</div>");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}

	/**
	 * @see javax.servlet.jsp.tagext.Tag#getParent()
	 */
	public Tag getParent() {
		return null;
	}

	/**
	 * @see javax.servlet.jsp.tagext.Tag#release()
	 */
	public void release() {
		url = null;
		param = null;
		urlStyle = null;
	}

	/**
	 * @see javax.servlet.jsp.tagext.Tag#setPageContext(javax.servlet.jsp.PageContext)
	 */
	public void setPageContext(PageContext context) {
		pageContext = context;
	}

	/**
	 * @see javax.servlet.jsp.tagext.Tag#setParent(javax.servlet.jsp.tagext.Tag)
	 */
	public void setParent(Tag arg0) {
	}
}
